package android.app.gfestival.soap;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Log;

public class SoapParserHelper {
	private static final String SOAP_ACTION = "http://www.halloapp.net/soap#";
	private static final String NAMESPACE = "http://www.halloapp.net/soap";
	private static final String URL = "http://www.halloapp.net/soap";
	
	public static List<Event> getAllEventById(String id)
			throws Exception {
		String methodName = "get_events_by_day_id";
		List<Event> events = new ArrayList<Event>();
		// set up request
		SoapObject request = new SoapObject(NAMESPACE, methodName); 
		//add artribute
		request.addProperty("event_day_id", "" + id); 
		
		Vector result = requestV(request, methodName);
		int length = result.size();
		for (int i = 0; i < length; i++) {
			Event event = new Event();
			Ksoap2ResultParser.parseBusinessObject(result.get(i)
					.toString(), event);
			events.add(event);
		}
		return events;
	}
	public static List<Artist> getArtistDetailById(String id)
	throws Exception {
		String methodName = "get_artist_detail_by_id";
		List<Artist> list = new ArrayList<Artist>();
		// set up request
		SoapObject request = new SoapObject(NAMESPACE, methodName); 
		//add artribute
		request.addProperty("artist_id", "" + id); 
		
		Vector result = requestV(request, methodName);
		int length = result.size();
		for (int i = 0; i < length; i++) {
			Artist obj = new Artist();
			Ksoap2ResultParser.parseBusinessObject(result.get(i)
					.toString(), obj);
			list.add(obj);
		}
		return list;
	}
	public static List<ArtistDescription> getAllArtistHaveEvents()
	throws Exception {
		String methodName = "get_all_artist_have_events";
		List<ArtistDescription> list = new ArrayList<ArtistDescription>();
		// set up request
		SoapObject request = new SoapObject(NAMESPACE, methodName); 
		Vector result = requestV(request, methodName);
		int length = result.size();
		for (int i = 0; i < length; i++) {
			ArtistDescription obj = new ArtistDescription();
			Ksoap2ResultParser.parseBusinessObject(result.get(i)
					.toString(), obj);
			if(obj.artist_id != null)
				list.add(obj);
			
			
		}
		return list;
	}
		
	public static List<EventDate> getAllDayHaveEvents()
	throws Exception {
		String methodName = "get_all_day_have_events";
		List<EventDate> list = new ArrayList<EventDate>();
		// set up request
		SoapObject request = new SoapObject(NAMESPACE, methodName); 
		
		Vector result = requestV(request, methodName);
		int length = result.size();
		for (int i = 0; i < length; i++) {
			EventDate obj = new EventDate();
			Ksoap2ResultParser.parseBusinessObject(result.get(i)
					.toString(), obj);
			if(obj.event_day_id != null )
				list.add(obj);
		}
		return list;
	}
//	
	
	private static Vector requestV(SoapObject request, String methodName) throws Exception{
		// put all required data into a soap envelope
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11); 
		envelope.setOutputSoapObject(request); // prepare request
//		envelope.dotNet = true;
		HttpTransportSE se = new HttpTransportSE(URL);
		se.debug = true;
		se.call(SOAP_ACTION + methodName, envelope); // send request
		Vector result = (Vector) envelope.getResponse();
		return result;
	}
	
	@SuppressWarnings(value = { "Can do this method" })
	private static SoapObject request(SoapObject request, String methodName) throws Exception{
		// put all required data into a soap envelope
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11); 
		envelope.setOutputSoapObject(request); // prepare request
//		envelope.dotNet = true;
		HttpTransportSE se = new HttpTransportSE(URL);
		se.debug = true;
		se.call(SOAP_ACTION + methodName, envelope); // send request
		SoapObject result = (SoapObject) envelope.bodyIn;// result
		return result;
	}
}
